package com.whut.monitor.controller;

import com.alibaba.fastjson.JSONObject;
import com.whut.monitor.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @author CFlower
 * 萤石云远程摄像头调用接口（解决跨域问题）
 */
@Controller
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private IVideoService videoService;

    @RequestMapping("/getAccessToken")
    @ResponseBody
    public JSONObject getAccessToken(String appKey, String appSecret) throws IOException {
        JSONObject j = videoService.getAccessToken(appKey, appSecret);
        return j;
    }

    @RequestMapping("/getDeviceStatus")
    @ResponseBody
    public boolean getDeviceStatus(String accessToken, String deviceSerial) throws IOException {
        JSONObject j = videoService.getDeviceInfo(accessToken, deviceSerial);
        int devStatus = Integer.parseInt(j.getString("status"));
        if (devStatus == 1) {
            // 在线
            return true;
        } else {
            // 不在线
            return false;
        }
    }

    @RequestMapping("/setVideoControl")
    @ResponseBody
    public boolean setVideoControl(String accessToken, String deviceSerial, int channelNo, int direction, int speed) throws IOException {
        JSONObject j = videoService.setVideoControl(accessToken, deviceSerial, channelNo, direction, speed);
        int operatorCode = Integer.parseInt(j.getString("code"));
        int rightCode = 200;
        if (operatorCode == rightCode) {
            // 成功
            return true;
        } else {
            // 不成功
            return false;
        }
    }

    @RequestMapping("/setVideoControlStop")
    @ResponseBody
    public boolean setVideoControlStop(String accessToken, String deviceSerial, int channelNo) throws IOException {
        JSONObject j = videoService.setVideoControlStop(accessToken, deviceSerial, channelNo);
        int operatorCode = Integer.parseInt(j.getString("code"));
        int rightCode = 200;
        if (operatorCode == rightCode) {
            // 成功
            return true;
        } else {
            // 不成功
            return false;
        }
    }

}
